/**************Author by NEO**************/

#import "TabBarController.h"
#import "BaseTabbar.h"
//#import "OneKeyHelpViewController.h"
#import "maosheng-Swift.h"

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface TabBarController()<BaseTabbarDelegate>
@property(nonatomic,retain) UIView * tabBarView;
@property(nonatomic,retain) UIImageView * tabBarImageView;
//-(id) initTabBarControllertabBarHeight:(CGFloat)barHeight withImages:(NSArray *) images withHightLightImages:(NSArray *) hightLightImages withTitles:(NSArray *) titles option:(BOOL) hasTitle;

@end

@implementation TabBarController

@synthesize tabBarView;
@synthesize tabBarImageView;





- (void)viewDidLoad
{
    [super viewDidLoad];
//    BaseTabbar *baseTabBar = [[BaseTabbar alloc] init];
//    baseTabBar.delegate = self;
//    [self setValue:baseTabBar forKey:@"tabBar"];
    
}

#pragma mark - BaseTabBarDelegate

- (void)baseTabbarOneKeyHelpAction {
//    UINavigationController *navi = self.childViewControllers[self.selectedIndex];
//    OneKeyHelpViewController *vc = [[OneKeyHelpViewController alloc] init];
//    [vc setHidesBottomBarWhenPushed:YES];
//    [navi pushViewController:vc animated:YES];
}

-(id) initTabBarControllertabBarHeightWithImages:(NSArray *) images withHightLightImages:(NSArray *) hightLightImages withTitles:(NSArray *)titles With:(NSArray *)ctrlArr;
{
    return [self initTabBarControllerWithImages:images withHightLightImages:hightLightImages withTitles:titles option:NO With:ctrlArr];
}

-(id) initTabBarControllerWithImages:(NSArray *) images withHightLightImages:(NSArray *) hightLightImages withTitles:(NSArray *) titles option:(BOOL) hasTitle With:(NSArray *)ctrlArr
{
    self = [super init];
//    UIImage * backgroundImage = [self imageWithColor:[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1]];
    UIImage * backgroundImage = [self imageWithColor:[UIColor whiteColor]];

    self.tabBar.backgroundImage = backgroundImage;
    self.newlabel = [[UILabel alloc] init];
    self.newlabel.backgroundColor = [UIColor redColor];
    self.newlabel.layer.cornerRadius = 4;
//    self.newlabel.font = Font(10);
    self.newlabel.textColor = [UIColor whiteColor];
    self.newlabel.textAlignment = NSTextAlignmentCenter;
    self.newlabel.layer.masksToBounds = YES;
    
    CGRect tabFrame = self.tabBar.frame;
    //确定小红点的位置
    float percentX = (1 + 0.6) / 5;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    self.newlabel.frame = CGRectMake(x, y, 8, 8);//圆形大小为10
    self.newlabel.hidden = YES;
    [self.tabBar addSubview:self.newlabel];
//    self.newlabel.hidden = YES;
    
    self.viewControllers = ctrlArr;
    if (self){
        NSArray * controllers = ctrlArr;
        for (int i = 0; i < controllers.count; i++) {
            UIViewController * controller = [controllers objectAtIndex:i];
//            controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:[titles objectAtIndex:i] image:nil tag:0];
//            [controller.tabBarItem setImageInsets:UIEdgeInsetsMake(5, 0, -5, 0)];
            UIImage *selectImg = [hightLightImages objectAtIndex:i];
            UIImage *unselectImg = [images objectAtIndex:i];
            if (selectImg) {
                selectImg = [selectImg  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            }
            if (unselectImg) {
                unselectImg = [unselectImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            }
//            [[controller tabBarItem] setFinishedSelectedImage:selectImg withFinishedUnselectedImage:unselectImg];
            controller.tabBarItem = [[UITabBarItem alloc]initWithTitle:[titles objectAtIndex:i] image:unselectImg selectedImage:selectImg];
            [[controller tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                             [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1], NSForegroundColorAttributeName, [UIFont fontWithName:@"PingFangSC-Regular" size:10], NSFontAttributeName,
                                                             nil] forState:UIControlStateNormal];
            
            [[controller tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                             [UIColor redColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"PingFangSC-Regular" size:10], NSFontAttributeName,
                                                             nil] forState:UIControlStateSelected];
         }
        

    }
    return self;
}
//根据颜色返回图片
-(UIImage*) imageWithColor:(UIColor*)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark - UITabBarDelegate
- (void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    self.selectedIndex = [tabBar.items indexOfObject:item];
    if(self.selectedIndex == 1){
//        self.newlabel.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
